package com.lemner.ke.base;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemner.ke.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 作者：杨烨
 * 时间：2018.9.15
 * 类描述：BaseActivity,Activity基类
 */

public abstract class BaseFragmentActivity extends BaseActivity {
    private LinearLayout ll_tab;
    private List<TextView> tvList = new ArrayList<>();
    private List<ImageView> ivList = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();
    private List<Integer> selectImageList = new ArrayList<>();
    private List<Integer> normalImageList = new ArrayList<>();
    private List<String> textList = new ArrayList<>();

    @Override
    public int getLayoutFile() {
        return R.layout.activity_base_fragment;
    }

    /**
     * 设置内容
     *
     * @param fragments，fragments的Arrays
     * @param texts，下方按钮上文字的Arrays
     * @param selectImages，下方按钮上被选择状态下图片的Arrays
     * @param normalImages，下方按钮上正常状态下图片的Arrays
     */
    public void set(Fragment[] fragments, String[] texts, Integer[] selectImages, Integer[] normalImages) {
        set(Arrays.asList(fragments), Arrays.asList(texts), Arrays.asList(selectImages), Arrays.asList(normalImages));
    }

    /**
     * 设置内容
     *
     * @param fragmentList，fragments的List
     * @param textList，下方按钮上文字的List
     * @param selectImageList，下方按钮上被选择状态下图片的List
     * @param normalImageList，下方按钮上正常状态下图片的List
     */
    public void set(List<Fragment> fragmentList, List<String> textList, List<Integer> selectImageList, List<Integer> normalImageList) {
        ll_tab = findViewById(R.id.ll_tab);
        this.fragmentList = fragmentList;
        this.textList = textList;
        this.selectImageList = selectImageList;
        this.normalImageList = normalImageList;
        addTag();
        select(0);
    }

    private void addTag() {
        for (int i = 0; i < textList.size(); i++) {
            LinearLayout linearLayout = new LinearLayout(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            linearLayout.setLayoutParams(lp);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setGravity(Gravity.CENTER);
            linearLayout.addView(addImageView(i));
            linearLayout.addView(addTextView(i));
            final int finalI = i;
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reset();
                    select(finalI);
                }
            });
            ll_tab.addView(linearLayout);
        }
    }

    private TextView addTextView(int i) {
        final TextView textView = new TextView(this);
        final String text = textList.get(i);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(lp);
        textView.setText(text);
        tvList.add(textView);
        return textView;
    }

    private ImageView addImageView(int i) {
        final ImageView imageView = new ImageView(this);
        imageView.setImageResource(normalImageList.get(i));
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(72, 72);
        imageView.setLayoutParams(lp);
        ivList.add(imageView);
        return imageView;
    }

    private void reset() {
        for (int i = 0; i < textList.size(); i++) {
            ivList.get(i).setImageResource(normalImageList.get(i));
        }
    }

    private void select(int position) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        ivList.get(position).setImageResource(selectImageList.get(position));
        transaction.replace(R.id.fl_container, fragmentList.get(position));
        transaction.commit();
    }
}